package cn.rocky.offer;

public class offer39 {

    /**
     * 摩尔投票 (众数)
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        int x = 0, votes = 0;
        for (int num: nums) {
            if (votes == 0) {
                x = num;
            }
            votes += num == x ? 1 : -1;
        }
        return x;
    }

    public static void main(String[] args) {
        // nums
        int[] nums = new int[]{1,2,3,2,2,3,2,1};
        offer39 offer39 = new offer39();
        int i = offer39.majorityElement(nums);
        System.out.println(i);
    }
}
